package com.template.templateproduce.produce.controller;

import com.template.templateproduce.api.UserApiClient;
import com.template.templateproduce.produce.service.ProduceService;
import com.template.templateproduce.user.dao.LoginUserDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/produce")
@RefreshScope
public class ProduceController {

    @Autowired
    ProduceService produceService;


    @Autowired
    UserApiClient userApiClient;

    @Autowired
    LoginUserDAO loginUserDAO;


    @Value("${produce.test}")
    private String produceTest;

    @RequestMapping("/save")
    public Long saveProduce(){
        return produceService.saveProduce();
    }
    @RequestMapping("/produce-test")
    public String getUerAge() {
        return produceTest;
    }
//http://127.0.0.1:8002/produce/produce-test
    @RequestMapping("/produce-add")
    public String produceAdd(@RequestParam("userId") String userId) {
        System.out.println("11");
        Authentication then =SecurityContextHolder.getContext().getAuthentication();
        System.out.println(then.getPrincipal().toString());
        System.out.println(loginUserDAO.findAllUser().get(0).getPassword());
        return produceService.produceAdd(userId);
    }


    @RequestMapping("/test")
    public String produceAdd2() {
        return userApiClient.getuserTtest();
    }

}
